package com.market.fleamarket.config;

import com.market.fleamarket.entity.User;
import com.market.fleamarket.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class DataInitializer implements CommandLineRunner {

    @Autowired
    private UserService userService;

    @Override
    public void run(String... args) throws Exception {
        // 检查admin用户是否存在
        if (!userService.findByUsername("admin").isPresent()) {
            // 创建管理员用户
            User adminUser = new User();
            adminUser.setUsername("admin");
            adminUser.setPassword("123456");  // 与数据库中一致
            adminUser.setRole("admin");
            userService.register(adminUser);
            System.out.println("已创建管理员用户: admin/123456");
        }
    }
}